package fxTrader.main;

import local_database.Statistic;
import local_database.XmlWriter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Scanner;

import java.util.Date;
import updater.currency.convertor.ReadRSS;
import updater.currency.data.Currency;
import updater.currency.data.CurrencyName;

import fxTrader.algorithm.TrendFinder;
import fxTrader.presentation.TrendReporter;
import fxTrader.retriever.CurrencyRetriever;

public class Main {

	public static void main(String args[]) {

		boolean isTrue = true;
		String userInput;
		int num = 0;
		Scanner scanner = new Scanner(System.in);

		menu();

		while (isTrue) {

			System.out
					.print("\nPlease enter the function code (1, 2, 3 and 0 to exit): ");
			userInput = scanner.next();

			try {
				num = Integer.parseInt(userInput);
				if (num < 0 || num > 3) {
					System.out
							.println("\n[WARNING] - invalid input, please try again.\n");
					menu();
					isTrue = true;
				} else
					isTrue = false;
			}

			catch (Exception e) {
				System.out
						.println("\n[WARNING] - invalid input, please try again.\n");
				menu();
				isTrue = true;
			}
		}

		while (num != 0) {

			if (num == 1) {
				System.out
						.println("\nFunction [1] - Real Time Currency Information");

				ReadRSS rs = new ReadRSS();
				ArrayList<CurrencyName> currencyNameList = rs
						.GetCurrencyNameList("currency_list");

				// *** Display Common Currency Menu *** //
				commonCurrencyMenu();

				boolean isCorrectCurrency = false;
				System.out.print("\nPlease select one of the currency: ");
				String cur = scanner.next();

				for (int i = 0; i < currencyNameList.size(); i++) {
					if (cur.equalsIgnoreCase(currencyNameList.get(i)
							.getSymbol()))
						isCorrectCurrency = true;
				}

				if (isCorrectCurrency) {
					Currency currency = CurrencyRetriever.getLatestCurrencyBySymbol(cur);
					System.out.println("Date: " + currency.getDate());
					System.out.println("Name: " + currency.getName());
					System.out.println("Price: " + currency.getPrice());
				} else
					System.out.println("\n[WARNING] - invaild currency, please try again.");
			}

			else if (num == 2) {
				System.out.println("\nFunction [2] - Currency Transaction Consultation");

				ReadRSS rs = new ReadRSS();
				ArrayList<CurrencyName> currencyNameList = rs.GetCurrencyNameList("currency_list");

				commonCurrencyMenu();

				boolean isCorrectCurrency = false;
				System.out.print("\nPlease select one of the currency: ");
				String cur = scanner.next();

				for (int i = 0; i < currencyNameList.size(); i++) {
					if (cur.equalsIgnoreCase(currencyNameList.get(i).getSymbol()))
						isCorrectCurrency = true;
				}

				if (isCorrectCurrency) {
					ArrayList<Currency> currency = CurrencyRetriever.getCurrencyListBySymbol(cur);
					boolean result;
					double[] list;

					// *** Display consultation for investment *** //
					String term;
					boolean isCorrectTerm = true;
					System.out
							.print("\nPlease select short or long (s/l) term investment: ");
					term = scanner.next();

					TrendFinder trendFinder = new TrendFinder();
					TrendReporter trendReporter = new TrendReporter();

					// *** Consultation for Short-Term Investment *** //
					if (term.equalsIgnoreCase("s")) {

						// Analyze the selected currency - short term
						result = trendFinder.MakeDecision_shortTrade(currency);
						list = trendFinder.getResultList(currency);

						// Display the consultation after analysis
						trendReporter.displayConsultation_shortTrend(result,
								list);
						isCorrectTerm = true;
					}

					// *** Consultation for Long-Term Investment *** //
					else if (term.equalsIgnoreCase("l")) {

						// Analyze the selected currency - long term
						result = trendFinder.MakeDecision_longTrade(currency);
						list = trendFinder.getResultList(currency);

						// Display the consultation after analysis
						trendReporter.displayConsultation_longTrend(result,
								list);
						isCorrectTerm = true;
					}

					// *** Handling invalid investment term *** //
					else {
						System.out
								.println("\n[WARNING] - invalid investment term, please try again.\n");
						isCorrectTerm = false;
					}
					

					if (isCorrectTerm) {
						System.out.print("\nDo you want to buy this currency (y/n): ");
						String reply;
						reply = scanner.next();

						// *** Make new transaction record *** //
						if (reply.equalsIgnoreCase("y")) {
							XmlWriter writer = new XmlWriter("UserStat.xml");

							writer.AddNewElementToRoot("Currency", "code", cur);

							Currency lastestCur = CurrencyRetriever.getLatestCurrencyBySymbol(cur);

							result = writer.AddNewElement("Currency", "code", cur, "buy_price",
									String.valueOf(lastestCur.getPrice()));

							DateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy hh:mm");
							Date date = new Date();

							result = writer.AddNewElement("Currency", "code", cur, "buy_time", 
									dateFormat.format(date));

							System.out.println("New Transaction record is created.");
							System.out.println("You buyed " + cur + "." + "(Price: $ " + lastestCur.getPrice()+ ")");
							System.out.println("\nThank you for using our Investment Consultation");
						} else if (reply.equalsIgnoreCase("n")) {
							System.out.println("\nThank you for using our Investment Consultation");
						}
						else
							System.out.println("\n[WARNING] - invalid decision, please try again.\n");	
					}

				} else
					System.out
							.println("\n[WARNING] - invaild currency, please try again.");

			}

			else if (num == 3) {
				System.out.println("\nFunction [3] - My Transaction Records");
				Statistic stat = new Statistic("UserStat.xml");
				stat.process();
			}

			System.out.println();
			menu();

			// Select function option again.
			isTrue = true;

			while (isTrue) {

				System.out
						.print("\nPlease enter the function code (1, 2, 3 and 0 to exit): ");
				userInput = scanner.next();

				try {
					num = Integer.parseInt(userInput);
					if (num < 0 || num > 3) {
						System.out
								.println("\n[WARNING] - invalid input, please try again.\n");
						menu();
						isTrue = true;
					} else
						isTrue = false;
				}

				catch (Exception e) {
					System.out
							.println("\n[WARNING] - invalid input, please try again.\n");
					menu();
					isTrue = true;
				}
			}

		}

		System.out.println("System exit...");

	} /* end of main */

	/* Function - Menu() display the system main menu options */
	public static void menu() {
		System.out.println("FX-Trader Consultation System");
		System.out.println("---------------------[Menu]----------------------");
		System.out.println("| [1] : Real Time Currency Information\t\t|");
		System.out.println("| [2] : Currency Transaction Consultation\t|");
		System.out.println("| [3] : My Transaction Records\t\t\t|");
		System.out.println("| [0] : Exit\t\t\t\t\t|");
	} /* end of menu */;

	/* Function - commonCurrencyMenu() display the common currency options */
	public static void commonCurrencyMenu() {
		System.out.println("\nCommon Currency Menu:");
		System.out
				.println("|CNY - Chinese Yuan \t\t | JPY - Japanese Yen\t\t|");
		System.out.println("|EUR - Euro \t\t\t | AUD - Australian Dollar\t|");
		System.out
				.println("|CAD - Canadian Dollar \t\t | USD - United States Dollar\t|");
		System.out
				.println("|GBP - British Pound Sterling \t | OTS - Other Currencies \t|");
	} /* end of commonCurrencyMenu */
}